package com.astronomy.spit.service;

import com.astronomy.entity.IdWorker;
import com.astronomy.spit.mapper.SpitMapper;
import com.astronomy.spit.pojo.Spit;
import org.springframework.aop.target.LazyInitTargetSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Copyright(C),2019-2020,CarryWLTao互联网工作室
 * FileName:SpitService
 * Author:  Administrator
 * Date:    2020-06-19 14:00
 * Description:
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间       版本号          描述
 */
@Service
@Transactional
public class SpitService {
    @Autowired
    private SpitMapper spitMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询所有
     *
     * @return
     */
    public List<Spit> findAll() {
        return spitMapper.findAll();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Spit findById(String id) {
        return spitMapper.findById(id).get();
    }

    /**
     * 新增
     *
     * @param spit
     */
    public void save(Spit spit) {
        //初始化数据
        spit.set_id(idWorker.nextId() + "");
        spit.setPublishtime(new Date());//发布日期
        spit.setVisits(0);//浏览量
        spit.setShare(0);//分享数
        spit.setThumbup(0);//点赞数
        spit.setComment(0);//回复数
        spit.setState("1");//状态
        if (spit.getParentid() != null && !"".equals(spit.getParentid())) {
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }
        spitMapper.save(spit);
    }

    /**
     * 修改
     *
     * @param spit
     */
    public void update(Spit spit) {
        spitMapper.save(spit);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        spitMapper.deleteById(id);
    }

    /**
     * 吐槽 父节点分页查询
     *
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentid, int page, int size) {
        //分页
        Pageable pageable = PageRequest.of(page - 1, size);
        return spitMapper.findByParentid(parentid, pageable);
    }

    /**
     * 吐槽点赞
     *
     * @param spitId
     */
    public void thumbup(String spitId) {
       /* //点赞
        Spit spit = spitMapper.findById(spitId).get();
        spit.setThumbup((spit.getThumbup() == null ?0:spit.getThumbup())+1);
        spitMapper.save(spit);*/

        //优化
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is("1"));
        Update update = new Update();
        update.inc("thumbup", 1);
        mongoTemplate.updateFirst(query, update, "spit");

    }
}
